// pages/video/video.js
import request from '../../utils/request'
Page({

  data: {
    navList: [],
    // 存储当前点击的导航
    navId: '',
    // 存储当前导航下的视频列表
    videoList: [],
    // 存储当前点击的视频id
    currentVid: '',
    // 存储播放过的视频信息
    songTimeList: [],
    flag:true,
    fresh:false,
  },
  onLoad(options) {
    this.getNavList()
  },
  // 获取导航列表
  async getNavList() {
    const re = await request('/video/group/list')
    // console.log(re);
    let navList = re.data.splice(0, 15).map(item => {
      return {
        id: item.id,
        name: item.name
      }
    })
    this.setData({
      navList,
      navId: navList[0].id
    })
    this.getVideoList()
  },
  // 点击更改导航
  changeNav(event) {
    // console.log(event);
    this.setData({
      navId: event.currentTarget.dataset.id
    })
    this.getVideoList()
  },
  // 点击获取视频列表
  async getVideoList() {
    wx.showLoading({
      title: '正在加载...',
    })
    let navId = this.data.navId
    // let videoList =[]
    const re = await request('/video/group', {
      id: navId
    })
    // console.log(re);
    let videoList = re.datas.map(item => {
      return {
        vid: item.data.vid,
        commentCount: item.data.commentCount,
        shareCount: item.data.shareCount,
        coverUrl: item.data.coverUrl,
        title: item.data.title,
        avatarUrl: item.data.creator.avatarUrl,
        nickname: item.data.creator.nickname,
        url: item.data.urlInfo.url,
      }
    })

    this.setData({
      videoList,
    })
    wx.hideLoading({
      success: (res) => {},
    })
  },
  // 点击播放视频
  handlerPlay(event) {
    // console.log(event);
    let vid = event.currentTarget.id
    if (!this.player) {
      // 判断是否有播放器,没有的话 创建 video 上下文 VideoContext 对象
      this.player = wx.createVideoContext(vid)
      // console.log(this.player);
      // 调用播放器上的play方法
      this.player.play()
      // console.log(this.player.seek());
      this.setData({
        currentVid: vid
      })
    } else {
      if (vid != this.data.currentVid) {
        // 说明与上次点击的视频不是同一个,先暂停之前的，再播放现在点击的
        this.player.pause()
        this.player = wx.createVideoContext(vid)
        let obj = this.data.songTimeList.find(item => item.id === vid)
        if (obj) {
          this.player.seek(obj.currentTime)
        }
        this.player.play()
        this.setData({
          currentVid: vid
        })
       
      } else {
        // 点击的是同一个
        if(this.data.flag){
          this.player.pause()
          this.setData({
            flag:false
          })
        }else{
          this.player.play()
          this.setData({
            flag:true
          })
        }
        
      }
    }
  },
  // 进度条发送变化时触发
  timerUpdate(event) {
    // console.log(event);
    let id = event.currentTarget.id
    let currentTime = event.detail.currentTime
    let songTimeList = this.data.songTimeList
    // 查看播放列表中是否有当前点击的视频
    let obj = songTimeList.find(item => item.id === id)
    if (obj) {
      // 如果有，只需要更新播放时间
      obj.currentTime = currentTime
    } else {
      // 如果没有就往播放列表中添加一个数据
      songTimeList.push({
        id,
        currentTime
      })
    }
    this.setData({
      songTimeList
    })
  },
  // 下拉刷新
  async  handerFresh(){
    console.log(11);
    this.setData({
      fresh : true
    })
    await this.getVideoList()
    this.setData({
      fresh:false
    })
  },
  // 上啦加载
  handerLoad(){
    let videoList = this.data.videoList
    if(videoList.length>= 20) return
    videoList.push(...videoList)
    this.setData({
      videoList 
    })
  },
  onShareAppMessage({from}){
    console.log(111);
      if(from=='button'){
        return {
           title:'给我砍一刀',
           path:'/pages/video/video',
           imageUrl:'/static/1.webp'
        }
    }else{
      //系统
      return {
       title:'砍一刀给你100块，骗人啊',
       path:'/pages/video/video',
       imageUrl:'/static/1.webp'
      }
    }
  }
})